package cn.keduox.service.impl;


import cn.keduox.dao.impl.BookDaoImpl;
import cn.keduox.dao.impl.BookRecDaoImpl;
import cn.keduox.dao.impl.BookRecTypeDaoImpl;
import cn.keduox.entity.Book;
import cn.keduox.entity.BookRec;
import cn.keduox.entity.BookRecType;
import cn.keduox.entity.PageBean;

import java.util.List;

/**
 * User: Yongman
 * Date: 2017/12/31
 * 图书推荐服务类
 */
public class BookRecTypeServiceImpl extends BaseServiceImpl{
    /**
     * 查询推荐图书
     * @return
     */
    public List<BookRecType> query(){
//        创建相应实体对象
        BookRecTypeDaoImpl bookRecTypeDaoImpl = new BookRecTypeDaoImpl();
        BookRecDaoImpl bookRecDaoImpl = new BookRecDaoImpl();
        BookDaoImpl bookDaoImpl = new BookDaoImpl();
        try {
//            查询推荐类型
            List<BookRecType> bookRecTypes = bookRecTypeDaoImpl.select(new BookRecType(),false,false,new PageBean(),false,null);
            if (bookRecTypes !=null && bookRecTypes.size()>0){
                for (BookRecType brt: bookRecTypes) {
//                    查询推荐类型对应的推荐书籍
                    String typeId = brt.getId();
                    BookRec bookRec = new BookRec();
                    bookRec.setType(typeId);
//                    根据类型查询
                    List<BookRec> bookRecList = bookRecDaoImpl.select(bookRec,false,false,new PageBean(),false,"hot");
                    if (bookRecList!=null && bookRecList.size()>0){
//                        将集合添加到BookRecType属性中
                        brt.setBookRecList(bookRecList);
                        for (BookRec br: bookRecList) {
//                            获取书籍id
                            String bookId = br.getBookId();
                            Book b = new Book();
                            b.setId(bookId);
//                            根据书籍id查询书籍信息
                            List<Book> bookList = bookDaoImpl.select(b,false,false,new PageBean(),false,null);
                            if (bookList!=null && bookList.size()>0){
                                br.setBook(bookList.get(0));
                            }
                        }
                    }
                }
                return bookRecTypes;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
